package cn.ffcs.uec.outStaff.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.dao.UecOutStaffInfomationDao;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfomationNew;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfomationVo;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecOutStaffInfomationDao")
@Transactional
public class UecOutStaffInfomationDaoImpl extends BaseDaoImpl implements UecOutStaffInfomationDao {

	@Override
	public UecOutStaffInfomationVo qryUecOutStaffInfomationByUecOutStaffInfomation(UecOutStaffInfomationVo uecOutStaffInfo) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_OUT_STAFF_INFOMATION T WHERE 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId()) && uecOutStaffInfo.getOutStaffId()>0) {
			sb.append("and T.out_Staff_Id = ?");
			params.add(uecOutStaffInfo.getOutStaffId());
		}
		if(!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPsnCode())) {
			sb.append("and T.PSNCODE = ?");
			params.add(uecOutStaffInfo.getPsnCode());
		}
		if (null != uecOutStaffInfo) {
			List<UecOutStaffInfomationVo> list = super.jdbcFindList(sb.toString(), params, UecOutStaffInfomationVo.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public List<UecOutStaffInfomationVo> qryUecOutStaffListByUecOutStaffInfo(UecOutStaffInfomationVo uecOutStaffInfoVo) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_OUT_STAFF_INFOMATION T WHERE 1=1");
		List<Object> params = new ArrayList<Object>();
		String orgName = PermissionUtil.getManagerOrg();
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnName())) {
			sb.append(" AND T.PSNNAME LIKE ? ");
			params.add("%"
					+ StringEscapeUtils.escapeSql(uecOutStaffInfoVo
							.getPsnName()) + "%");
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnCode())) {
			sb.append(" AND T.PSNCODE = ? ");
			params.add(uecOutStaffInfoVo.getPsnCode());
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getIdNum())) {
			sb.append(" AND T.IDNUM = ? ");
			params.add(uecOutStaffInfoVo.getIdNum());
		}
		
		if (!"区本部".equals(orgName)) {
			sb.append(" AND T.ORGNAME LIKE ? ");
			params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
		}
		return this.jdbcFindList(sb.toString(), params, UecOutStaffInfomationVo.class);
	}

	@Override
	public PageInfo queryPageInfoByUecOutStaffInfomationVo(UecOutStaffInfomationVo uecOutStaffInfoVo, int current,
			int pageSize) {
		if (null != uecOutStaffInfoVo) {
			String orgName = PermissionUtil.getManagerOrg();
			StringBuilder sb = new StringBuilder(
					" SELECT T.* FROM UEC_OUT_STAFF_INFOMATION T "
					+" INNER join LOG_DOWN T1 ON(t.staffId = t1.pk_psndoc) "
					+" WHERE t1.signno='2' AND t1.openswitch='Y' ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnName())) {
				sb.append(" AND T.PSNNAME LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(uecOutStaffInfoVo
								.getPsnName()) + "%");
			}
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnCode())) {
				sb.append(" AND T.PSNCODE = ? ");
				params.add(uecOutStaffInfoVo.getPsnCode());
			}
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getIdNum())) {
				sb.append(" AND T.IDNUM = ? ");
				params.add(uecOutStaffInfoVo.getIdNum());
			}
			
			if (!"区本部".equals(orgName)) {
				sb.append(" AND T.ORGNAME LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
			}
			sb.append(" ORDER BY T.ORGNAME");
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecOutStaffInfomationVo.class);
		}
		return null;
	}
	@Override
	public List<UecOutStaffInfomationNew> qryUecOutStaffList(UecOutStaffInfomationNew uecOutStaffInfoVo) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_OUT_STAFF_INFOMATION T WHERE 1=1");
		List<Object> params = new ArrayList<Object>();
		String orgName = PermissionUtil.getManagerOrg();
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnName())) {
			sb.append(" AND T.PSNNAME LIKE ? ");
			params.add("%"
					+ StringEscapeUtils.escapeSql(uecOutStaffInfoVo
							.getPsnName()) + "%");
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnCode())) {
			sb.append(" AND T.PSNCODE = ? ");
			params.add(uecOutStaffInfoVo.getPsnCode());
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getIdNum())) {
			sb.append(" AND T.IDNUM = ? ");
			params.add(uecOutStaffInfoVo.getIdNum());
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getForgname())) {
			sb.append(" AND T.FORGNAME = ? ");
			params.add(uecOutStaffInfoVo.getForgname());
		}
		
		if (!"区本部".equals(orgName)) {
			sb.append(" AND T.ORGNAME LIKE ? ");
			params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
		}
		return this.jdbcFindList(sb.toString(), params, UecOutStaffInfomationNew.class);
	}

	@Override
	public List<Map<String, Object>> qryDataList(UecOutStaffInfomationNew uecOutStaffInfoVo) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM VIEW_OUT_STAFF_INFOMATION T WHERE 1=1"
				);
		List<Object> params = new ArrayList<Object>();
		String orgName = PermissionUtil.getManagerOrg();
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnName())) {
			sb.append(" AND T.姓名 LIKE ? ");
			params.add("%"
					+ StringEscapeUtils.escapeSql(uecOutStaffInfoVo
							.getPsnName()) + "%");
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getPsnCode())) {
			sb.append(" AND T.编码 = ? ");
			params.add(uecOutStaffInfoVo.getPsnCode());
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getIdNum())) {
			sb.append(" AND T.身份证号 = ? ");
			params.add(uecOutStaffInfoVo.getIdNum());
		}
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfoVo.getForgname())) {
			sb.append(" AND T.上级组织 = ? ");
			params.add(uecOutStaffInfoVo.getForgname());
		}
		
		if (!"区本部".equals(orgName)) {
			sb.append(" AND T.上级组织 LIKE ? ");
			params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
		}
		return this.getJdbcTemplate().queryForList(sb.toString(), params.toArray());
	}
}
